const process = require("process");
const fs = require("fs");
const path = require("path");
const chalk = require("chalk").default;
const readlineSync = require("readline-sync");
const beautify = require("json-beautify");

let cwd = process.cwd();

function SetUp() {
    console.log(chalk.greenBright("Setup mail-all-emls in this folder..."));
    let SMTP_SERVER = readlineSync.question("Enter the host of SMTP SERVER : ");
    let SMTP_PORT = readlineSync.questionInt("Enter the port for SMTP SERVER : ");
    let SMTP_USE_TLS_SECURE = readlineSync.keyInYN("Should I use TLS when connecting to server ? ");
    let RECEIVERS = readlineSync.question("Who do you want to send mails to (seprate with , )? \n");
    let YOUR_EMAIL_ADDRESS = readlineSync.question("What's your username for this mailbox ?");
    let config = {
        SMTP_SERVER,
        SMTP_PORT,
        SMTP_USE_TLS_SECURE,
        RECEIVERS,
        YOUR_EMAIL_ADDRESS
    }
    SetUp.write(config);
    console.log(chalk.greenBright("setup done!"));
}

SetUp.write = function (config) {
    fs.writeFileSync(
        path.join(cwd, ".mail-all-emls.json"),
        beautify(config, null, 2, 100), {
            encoding: "utf8",
            flag: "w"
        }
    );
}

module.exports = {
    SetUp
}